﻿using System.Collections.Generic;
using Budgetsch.Web.EntityModel;
using Budgetsch.Web.Services;
using GalaSoft.MvvmLight;

namespace Budgetsch.ViewModels.Profile
{

    public class ProfileVM : ViewModelBase
    {
        private readonly BudgetschDomainContext m_context;
        private User m_currentUser;
        private bool m_isSharing;
        private Currency m_selectedCurrency;

        public bool IsSharing
        {
            get { return m_isSharing; }
            set
            {
                m_isSharing = value;
                m_currentUser.IsSharing = value;
                m_context.SubmitChanges();
            }
        }

        public IEnumerable<Currency> Currencies { get; set; }

        public Currency SelectedCurrency
        {
            get { return m_selectedCurrency; }
            set
            {
                m_selectedCurrency = value;
                m_currentUser.Currency = value;
                m_context.SubmitChanges();
            }
        }

        public ProfileVM()
        {
            m_context = new BudgetschDomainContext();
            m_context.Load(m_context.GetCurrentUserQuery(), o =>
                                                                {
                                                                    foreach (var user in m_context.Users)
                                                                    {
                                                                        m_currentUser = user;
                                                                    }
                                                                }, new object());
            Currencies = m_context.Currencies;
            m_context.Load(m_context.GetCurrenciesQuery());
        }
    }
}